<?php

#################################################################
#								HTML
#################################################################

/**
 * Button component registed as 'element/button' 
 * 
 * $vars['text']
 * $vars['title']
 * $vars['id']
 * $vars['class']
 * $vars['color']
 * $vars['precall']		JS function that MUST return a boolean value (TRUE|FALSE) 
 * 
 * CONTROL
 * $vars['one_click']
 * 
 * POSIBLE ACTIONS (only one)
 * $vars['action']	//Calls a javascript method
 * $vars['url']		//Goes to a URL
 
 * $vars['submit']	//Submits a form
 */

if($_CONTEXT=='HTML'){

	$_CSS_STACK->push(blg_get_site_url().'element/button.css', TRUE);
	$_JS_STACK->push(blg_get_site_url().'element/button.js', TRUE);

	//PREPARE DEFAULTS
	if(!is_bool($vars['submit']))
		$vars['submit']= FALSE;
		
	if(!is_bool($vars['one_click'])){
		if($vars['submit'])
			$vars['one_click']= TRUE;
		else	
			$vars['one_click']= FALSE;
	}

	if(!isset($vars['precall']))
		$vars['precall']= FALSE;
		
	if(empty($vars['id']))
		$vars['id']= 'button_'.random_string(12);

	if(empty($vars['color']))
		$vars['color']= 'blue';		

	$class= 'blg_button';
	
	switch($vars['color']){
		case '1': 
		case 'blue': 
			$class= $class.' blg_button_1'; $class_num= '1'; break;		
		case '2': 
		case 'grey': 
			$class= $class.' blg_button_2'; $class_num= '2'; break;
		default:
			$class= $class.' blg_button_1'; $class_num= '1'; break;
	}
	
	//BUILD
	$button = '<div id="'.$vars['id'].'" blg_id="'.$vars['id'].'"';
	
	if(!empty($vars['class']))
		$button.= ' class="'.$class.' '.$vars['class'].'"';
	else
		$button.= ' class="'.$class.'"';
		
	if(!empty($vars['title']))
		$button.= ' title="'.$vars['title'].'"';
		
	if(!empty($vars['submit']))
		$button.= ' submit="submit_'.$vars['id'].'"';				
	elseif(!empty($vars['url']))
		$button.= ' url="'.$vars['url'].'"';
		
	$button.= '>';
	
	$button.= $vars['text'];
	
	$button.= '</div>';

	if(!empty($vars['submit']))
		$button.= '<input value="" class="blg_hide_submit" id="submit_'.$vars['id'].'" type="submit">';
	
	echo $button;
	
	/****************** APPEND JS CODE ********************/
	
	if(!empty($vars['action'])){
		$code="
			$('#".$vars['id']."').bind('click', function(){
				".add_trainling_slash($vars['action'], ';')."
			});
		";
		
		$_JS_CODE->append($code, ON_DOC_READY);
	}
	
	if($vars['one_click']){
		$code="
			$('div[blg_id=".$vars['id']."]').each(function(){
				$(this).bind('click', function(event){
					".($vars['precall']!==FALSE?'if('.$vars['precall'].'){':'')."
						".($vars['submit']?'blg_button_submit_all($(this));':'blg_button_disabled($(this))')."
					".($vars['precall']!==FALSE?'}':'')."
				});			
			});
		";

		$_JS_CODE->append($code, ON_DOC_READY);
	}	
		
	
}
#################################################################
#								JS
#################################################################
elseif($_CONTEXT=='JS'){ ?>

jQuery.preLoadImages("<?php echo blg_get_site_url()?>_graphics/default/buttons/bg1_hover.png", "<?php echo blg_get_site_url()?>_graphics/default/buttons/bg2_hover.png");

function blg_button_submit(button){
	button.css('color', '<?php echo blg_color('grey', '9');?>');
	button.css('cursor', 'wait');				
	button.unbind('click');	
}

function blg_button_submit_all(button){
	var form = button.closest('form');
	var formId= form.attr('id');
	$('#'+formId).find('div[submit]').each(function(){
		blg_button_submit($(this));	
	});
}

function blg_button_disabled(button){
	button.css('color', '<?php echo blg_color('grey', '9');?>');
	button.css('cursor', 'no-drop');				
	button.unbind('click');
}

$(document).ready( function(){
	$('.blg_button[submit]').bind('click', function(){
		$('#'+$(this).attr('submit')).click();
	});
	
	$('.blg_button[url]').bind('click', function(){
		window.location.href= $(this).attr('url');
	});	
});

<?php }
#################################################################
#								CSS
#################################################################
elseif($_CONTEXT=='CSS'){ ?>

div.blg_button{
	display: inline;
	cursor: pointer;
	padding: 4px 9px;			
	height: 23px;
	line-height: 23px;
	
	color: <?php echo blg_color('white', 'txt_button');?>;
	font-size: 10px;
	font-family: "Verdana";
	letter-spacing: 0px;
	
	background-position: center middle;	
	border-radius: 5px;		
}
div.blg_button:hover{
	color: <?php echo blg_color('white', 'white');?>;
}

div.blg_button_1{
	background-image: url('<?php echo blg_get_site_url()?>_graphics/default/buttons/bg1.png');
}
div.blg_button_1:hover{
	background-image: url('<?php echo blg_get_site_url()?>_graphics/default/buttons/bg1_hover.png');
}

div.blg_button_2{
	background-image: url('<?php echo blg_get_site_url()?>_graphics/default/buttons/bg2.png');
}
div.blg_button_2:hover{
	background-image: url('<?php echo blg_get_site_url()?>_graphics/default/buttons/bg2_hover.png');
}

input.blg_hide_submit{
	position: absolute;
	width: 1px;
	height: 1px;
	top: -9999px;
	left: -9999px;	
}
<?php }
